﻿<Page
    x:Class="MusicExplorer.Views.HomePageView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:caliburn="using:Caliburn.Micro"
    xmlns:converters="using:MusicExplorer.Converters"
    xmlns:common="using:MusicExplorer.Common">
    
    <Page.Resources>
        <converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
        <converters:NullToInvertVisibilityConverter x:Key="NullToInvertVisibilityConverter" />
        <converters:NullToBooleanConverter x:Key="NullToBooleanConverter" />
        <converters:InvertBooleanToVisibilityConverter x:Key="InvertBooleanToVisibilityConverter" />
        <converters:NullOrEmptyToVisibilityConverter x:Key="NullOrEmptyToVisibilityConverter" />
        
        <DataTemplate x:Key="ItemTemplate">
            <Border BorderThickness="1" BorderBrush="{StaticResource DefaultApplicationBorderTileBrush}">
                <Grid>
                    <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
                        <Image Source="{Binding Image}" Stretch="UniformToFill"
                               HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>

                    <Border VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}" 
                            Padding="2"> <!--Visibility="{Binding Group.DisplayNameOnHomeScreen, Converter={StaticResource BooleanToVisibilityConverter}}"-->
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <!--<Grid.ColumnDefinitions>
                                <ColumnDefinition Width="6.7*" />
                                <ColumnDefinition Width="3.3*" />
                            </Grid.ColumnDefinitions>-->

                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <TextBlock Text="{Binding DisplayName}" VerticalAlignment="Center"
                                    Foreground="{StaticResource DefaultApplicationBackground}"
                                    Style="{StaticResource BaseTextBlockStyle}"
                                    FontWeight="SemiBold" LineHeight="13" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
                                <TextBlock VerticalAlignment="Center" Grid.Column="1" Visibility="Collapsed"
                                    Foreground="{StaticResource DefaultApplicationBackground}"
                                    Style="{StaticResource BaseTextBlockStyle}" Margin="0, 0, 2, 0"
                                    FontWeight="SemiBold" LineHeight="13" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis">
                                    <Run Text="(" />
                                    <Run Text="{Binding NbNewTracks}" />
                                    <Run Text=")" />
                                </TextBlock>
                            </Grid>
                            
                            <Grid Grid.Row="1" Margin="0, 2, 0, 0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                            
                                <TextBlock Text="{Binding Group.QueryType}" Foreground="{StaticResource DefaultApplicationSecondaryForeground}" 
                                        Style="{StaticResource BaseTextBlockStyle}" FontSize="13" FontWeight="SemiBold"
                                        LineHeight="13" VerticalAlignment="Center" Margin="0, 0, 0, 2" />
                                <ItemsControl ItemsSource="{Binding RepositoryIcons}" HorizontalAlignment="Right"
                                              Grid.Column="1" VerticalAlignment="Center">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Image Stretch="Fill" Source="{Binding}" Margin="1"
                                                   HorizontalAlignment="Center" VerticalAlignment="Center" />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Height="15" />
                                            <!--<WrapGrid Orientation="Vertical" FlowDirection="RightToLeft" ItemHeight="15" ItemWidth="15"
                                                      HorizontalAlignment="Right" MaximumRowsOrColumns="2" />-->
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </Grid>
                        </Grid> 
                    </Border>

                    
                    <!--<StackPanel VerticalAlignment="Bottom" Visibility="{Binding Group.DisplayNameOnHomeScreen, Converter={StaticResource BooleanToVisibilityConverter}}"
                                Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
                        <TextBlock Text="{Binding DisplayName}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource BaseTextBlockStyle}" Height="60" Margin="15,0,15,0" FontWeight="SemiBold"/>
                    </StackPanel>-->
                </Grid>
            </Border>
        </DataTemplate>
    </Page.Resources>

    <Page.BottomAppBar>
        <CommandBar Height="92" IsSticky="True" IsOpen="{Binding SelectedGroup, Converter={StaticResource NullToBooleanConverter}}">
            <AppBarButton Label="Resize" Icon="ReShare" Visibility="{Binding SelectedGroup, Converter={StaticResource NullToVisibilityConverter}}">
                <AppBarButton.Flyout>
                    <MenuFlyout>
                        <MenuFlyoutItem Text="Big tile" caliburn:Message.Attach="[Event Click] = [SetTileSize($source)]" Tag="big" />
                        <MenuFlyoutItem Text="Wide tile" caliburn:Message.Attach="[Event Click] = [SetTileSize($source)]" Tag="wide" />
                        <MenuFlyoutItem Text="Small tile" caliburn:Message.Attach="[Event Click] = [SetTileSize($source)]" Tag="small" />
                    </MenuFlyout>
                </AppBarButton.Flyout>
            </AppBarButton>
            <AppBarToggleButton Label="Personalize" Icon="View"
                              caliburn:Message.Attach="[Event Click] = [GoInPersonalizedMode]" />
            
            <CommandBar.SecondaryCommands>
                <AppBarButton Label="New search" Icon="Add"
                              caliburn:Message.Attach="[Event Click] = [AddGroup]" />
                <AppBarButton Label="Edit search" Icon="Edit" Visibility="{Binding SelectedGroup, Converter={StaticResource NullToVisibilityConverter}}"
                              caliburn:Message.Attach="[Event Click] = [EditGroup]" />
                <AppBarButton Label="Delete search" Icon="Delete" Visibility="{Binding SelectedGroup, Converter={StaticResource NullToVisibilityConverter}}"
                              caliburn:Message.Attach="[Event Click] = [DeleteGroup]" />
            </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>

    <Grid Background="{StaticResource DefaultApplicationBackground}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid x:Name="progressGrid" VerticalAlignment="Top">
            <ProgressBar IsIndeterminate="True" Height="10" />
        </Grid>
        
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="GoBack" Style="{StaticResource BackButtonStyle}" />
            <TextBlock x:Name="CompleteTitle" Style="{StaticResource PageHeaderTextStyle}"
                       Foreground="{StaticResource DefaultApplicationForeground}" Grid.Column="1" />
        </Grid>

        <common:VariableGridView CanDragItems="True" CanReorderItems="True" ItemsSource="{Binding}"
                                 caliburn:Message.Attach="[Event BeforeDrop] = [UpdateGroups($eventArgs)];[Event Drop] = [RemoveEmptyCategory($eventArgs)];[Event ItemClick] = [NavigateToTracks($eventArgs)]"
                                 AllowDrop="True" AllowNewGroup="True" SelectionMode="Single" IsSwipeEnabled="True" x:Name="groupGrid"
                                 IsItemClickEnabled="True" Grid.Row="1" ItemTemplate="{StaticResource ItemTemplate}">
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
            <GridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid>
                                <TextBlock Text="{Binding Name, Mode=TwoWay}" Visibility="{Binding Parent.InPersonalizedMode, Converter={StaticResource InvertBooleanToVisibilityConverter}}"
                                       Margin="10, 10, 10, 5" Style="{StaticResource DefaultApplicationGroupTitleStyle}" Height="38" />
                                <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="Transparent" BorderThickness="0" x:Name="textboxHeader"
                                         Background="{StaticResource DefaultBackButtonBackgroundOver}" Padding="6, 0, 50, 0" Margin="3, 12, 10, 3"
                                         Visibility="{Binding Parent.InPersonalizedMode, Converter={StaticResource BooleanToVisibilityConverter}}"
                                         FontSize="26.667" FontWeight="Light" Height="38">
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="FocusStates">
                                            <VisualState x:Name="Focused">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="White"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                </TextBox>
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="GroupItem">
                            <Setter Property="Margin" Value="30, 0, 0, 0" />
                        </Style>
                    </GroupStyle.ContainerStyle>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid ItemHeight="150" ItemWidth="150" Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </GridView.GroupStyle>
            <GridView.ItemContainerStyle>
                <Style TargetType="GridViewItem">
                    <Setter Property="Margin" Value="0, 0, -7, -7" />
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                </Style>
            </GridView.ItemContainerStyle>
        </common:VariableGridView>
    </Grid>
</Page>
